/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.lcfgly.Service;

import com.lcfgly.CustomBean.PasswordChangeForm;
import com.lcfgly.CustomBean.SimpleUserInferForm;
import com.lcfgly.CustomBean.User;
import com.lcfgly.Dao.ImgsImpl;
import com.lcfgly.Dao.UserImpl;
import com.lcfgly.Dao.UserconuntImpl;
import com.lcfgly.Database.ShopImgs;
import com.lcfgly.Database.UserConunt;
import com.lcfgly.Database.UserInfo;
import com.lcfgly.Exception.ActionException;
import com.lcfgly.Exception.ErrorException;
import com.lcfgly.Exception.SQLEmptyException;
import com.lcfgly.Interface.GetCacheMark;
import com.lcfgly.Interface.UpdateCacheMark;
import com.lcfgly.util.Encode;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 用户操作
 * @author lcfgly
 */
@Service
public class UserService implements Serializable{
    
    Logger log=Logger.getLogger("SqlError");
    
    @Autowired
    UserImpl userImpl;
    
    @Autowired
    UserconuntImpl userinferImpl;
    
    @Autowired
    ImgsImpl imgsImpl;
    
    /**
     * 获取用户信息
     * @param id 用户ID
     * @return
     * @throws ActionException 
     */
    @GetCacheMark("userbyid_${0}")
    public User findById(int id)throws ActionException{
        User user=new User();
        try{
            user.setUsers(userImpl.findById(id));
            user.setUserinfer(userinferImpl.findByUid(id));
        }catch(SQLException e){
            log.error(e);
            throw new ActionException("查找不到先关数据");
        }
        List<ShopImgs> imgs= new ArrayList<ShopImgs>();
        try{
            imgs.addAll(imgsImpl.findByUserId(id));
        }catch(SQLEmptyException e){
            log.error(e);
            //查找不到数据
        }catch(SQLException e){
            log.error(e);
            throw new ActionException(e);
        }
        if(imgs.size()<3){
            int s=3-imgs.size();
            for(int i=0;i<s; i++)
            {
                ShopImgs img=new ShopImgs();
                img.setUserid(id);
                imgs.add(img);
            }
        }
        user.setImgs(imgs);
        return user;
    }
    
    
    /**
     * 获取用户信息
     * @param username 用户名称
     * @return
     * @throws ActionException 
     */
    @GetCacheMark("shopuserbyusername_${0}")
    public User findByUsername(String username)throws ActionException{
        User user=new User();
        try{
            user.setUsers(userImpl.findShopbyusername(username));
            user.setUserinfer(userinferImpl.findByUid(user.getUsers().getUid()));
        }catch(SQLException e){
            log.error(e);
            throw new ActionException("查找不到先关数据");
        }
        List<ShopImgs> imgs= new ArrayList<ShopImgs>();
        try{
            imgs.addAll(imgsImpl.findByUserId(user.getUsers().getUid()));
        }catch(SQLEmptyException e){
            log.error(e);
            //查找不到数据
        }catch(SQLException e){
            log.error(e);
            throw new ActionException(e);
        }
        if(imgs.size()<3){
            int s=3-imgs.size();
            for(int i=0;i<s; i++)
            {
                ShopImgs img=new ShopImgs();
                img.setUserid(user.getUsers().getUid());
                imgs.add(img);
            }
        }
        user.setImgs(imgs);
        return user;
    }
    
    
    /**
     * 保存用户数据
     * @param user
     * @param userid
     * @return
     * @throws ActionException 
     */
    @UpdateCacheMark("Imgs_${1}")
    public boolean Save(User user,int userid)throws ActionException{
        UserInfo users=user.getUsers();
        UserConunt userinfer=user.getUserinfer();
        List<ShopImgs> imgs=user.getImgs();
        try{
            userImpl.update(users,userid);
            userinferImpl.update(userinfer,userid);
        }catch(SQLException e){
            log.error(e);
            throw new ActionException(e);
        }
        for (ShopImgs imgs1 : imgs) {
            imgs1.setUpdatetime(new Date());
            saveimg(imgs1);
        }
        return true;
    }
    
    /**
     * 保存图片
     * @param arg0
     * @return
     * @throws ActionException 
     */
    private boolean saveimg(ShopImgs arg0) throws ActionException{
        if(arg0 == null)
            return false;
        else if(arg0.getImgurl() == null)
            return false;
        else if(arg0.getUserid()== 0)
            throw new ActionException("未检测到用户信息");
        else if(arg0.getUpdatetime()== null)
            arg0.setUpdatetime(new Date(new Date().getTime()));
        if(arg0.getId() == null){
            try{
                return imgsImpl.insert(arg0) == 1;
            }catch(SQLException e){
                log.error(e);
                throw new ErrorException();
            }
        }else{
            try{
                return imgsImpl.update(arg0) == 1;
            }catch(SQLException e){
                log.error(e);
                throw new ErrorException();
            }
        }
    }
    
    /**
     * 修改密码
     * @param form
     * @param id
     * @return 
     */
    public boolean passwordchange(PasswordChangeForm form,int id){
        try{
        UserInfo user=userImpl.findById(id);
        String pad=Encode.passwordcode(form.getOldpassword(), user.getSolt());
            if(pad == null ?false: pad.equals(user.getPassword())){
                user.setSolt(Encode.randomString(4));
                user.setPassword(Encode.passwordcode(form.getNewpassword(), user.getSolt()));
                userImpl.update(user, id);
                return true;
            }
            return false;
        }catch(SQLException e){
            log.error(e);
            return false;
        }
    }
    
    /**
     * 简易用户信息修改
     * @param userInferForm
     * @param id
     * @return 
     */
    public boolean SimpleUserinfer(SimpleUserInferForm userInferForm,int id){
        try{
            UserConunt userinfer=userinferImpl.findByUid(id);
            userinfer.setCssstyle(userInferForm.getCssstyle());
            userinfer.setEmail(userInferForm.getEmail());
            userinfer.setTel(userInferForm.getTel());
            userinfer.setPhone(userInferForm.getPhone());
            userinferImpl.update(userinfer, id);
            return true;
        }catch(SQLException e){
            log.error(e);
            return false;
        }
    }
    
    /**
     * 获取用户信息
     * @param id
     * @return
     * @throws SQLException 
     */
    public UserInfo findUsersById(int id)throws SQLException{
           return userImpl.findById(id);
    }
    
}
